package com.xiaofeng.Hello;

import java.util.Scanner;

public class Java11 {
    public static void main(String[] args) {

        Scanner sc =new Scanner(System.in);

        double arr[]={12.9, 53.54, 75.0, 99.1, 3.14};
        //数组中找最小的元素并打印输出在终端上
        System.out.println("arr数组中最小的元素的值："+find_min(arr));
        int arr1[]={2, 1, 3, 5, 4};
        System.out.println("arr1数组中的数据组合之后的结果:"+combine(arr1));

        for (int i = 0; i < arr1.length; i++) {
            System.out.print(arr1[i]+" ");
        }
        System.out.println();
        int data=sc.nextInt();
        arr1=insert_data(arr1,data);
        System.out.println("插入数据之后的数组：");
        for (int i = 0; i < arr1.length; i++) {
            System.out.printf(arr1[i]+" ");
        }
        System.out.println();
        int arr2[]={1};
        arr_cut(arr2);
        System.out.println("奇数放在数组的左边，偶数放在数组的右边：");
        for (int i = 0; i < arr2.length; i++) {
            System.out.printf(arr2[i]+" ");
        }
        System.out.println();

    }
    //找数组中最小的元素
    public static double find_min(double[] arr){
        double min =arr[0];
        for (int i = 0; i < arr.length; i++) {
            if(arr[i]<min){
                min =arr[i];
            }
        }
        return min;
    }
    //从前往后组合数组
    public static int combine(int arr1[]){
        int sum=0;
        for (int i = 0; i < arr1.length; i++) {
            sum =sum*10+arr1[i];
        }
        return sum;
    }
    //冒泡排序
    public static void bubble_sort(int[] arr){
        for (int i = 0; i < arr.length-1; i++) {
            int flag=1;
            for (int j = 0; j < arr.length-i-1; j++) {
                if(arr[j]>arr[j+1]){
                    int temp=arr[j];
                    arr[j]=arr[j+1];
                    arr[j+1]=temp;
                    flag=0;
                }
            }
            if(flag==1)
                break;
        }
    }
    //在数组中插入数据(按照升序或这降序排列)
    public static int[] insert_data(int[] arr,int data){
        int []new_arr=new int [arr.length+1];
        for (int i = 0; i < arr.length; i++) {
            new_arr[i]=arr[i];
        }
        new_arr[arr.length]=data;
        bubble_sort(new_arr);
        return new_arr;
    }
    //定义一个数组其中包含多个数字。奇数放在数组的左边，偶数放在数组的右边。
    public static void arr_cut(int arr[]){
        int left =0,right = arr.length-1;
        while(left<right){
            if(arr[left]%2!=0){
                left++;
            }
            if(arr[right]%2==0){
                right--;
            }
            //交换两个元素
            if(left<right)
            {
                int temp =arr[left];
                arr[left]=arr[right];
                arr[right]=temp;
                left++;
                right--;
            }
        }
    }
}
